#include "MWTimer.h"

#ifdef MW_PLATFORM_WIN32
#include <windows.h>
#else
#error not suppored.
#endif

using namespace Myway;

#define ON				1
#define OFF				2
#define SUPENDED		4

#define SAMPLE_TIME_L(t) { t = timeGetTime(); }

Timer::Timer()
{
	StartTime = 0;
	StopTime = 0;
	State = OFF;
}

Timer::~Timer()
{
}

void Timer::Start()
{
	SAMPLE_TIME_L(StartTime);
	StopTime = StartTime;
	State = ON;
}

void Timer::Stop()
{
	if (State == ON)
	{
		SAMPLE_TIME_L(StopTime);
		State = OFF;
	}
}

void Timer::Supend()
{
	if (State == ON)
	{
		SAMPLE_TIME_L(StopTime);
		State = SUPENDED;
	}
}

void Timer::Resume()
{
	if (State == SUPENDED)
	{
		unsigned int elapsed; 
		SAMPLE_TIME_L(elapsed);
		StartTime += elapsed - StopTime;
		State = ON;
	}
}

float Timer::ElapsedTime()
{
	if (State != ON)
		return float(StopTime - StartTime) / 1000.0f;
	else 
	{
		SAMPLE_TIME_L(StopTime);
		return float(StopTime - StartTime) / 1000.0f;
	}
}


//SYSTEMTIME SysTime;
//
//SystemTimer gSysTimer;
//
//SystemTimer::SystemTimer()
//{
//	timeBeginPeriod(1);
//}
//
//SystemTimer::~SystemTimer()
//{
//	timeEndPeriod(1);
//}
//
//void SystemTimer::Setup()
//{
//#ifdef MW_PLATFORM_WIN32
//
//    ::GetSystemTime(&SysTime);
//    ::SystemTimeToTzSpecificLocalTime(NULL, &SysTime, &SysTime);
//
//#else
//
//    #error not suppored.
//
//#endif
//}
//
//int SystemTimer::Year()
//{
//    return (int)SysTime.wYear;
//}
//
//int SystemTimer::Month()
//{
//    return (int)SysTime.wMonth;
//}
//
//int SystemTimer::Day()
//{
//    return (int)SysTime.wDay;
//}
//
//int SystemTimer::Hour()
//{
//    return (int)SysTime.wHour;
//}
//
//int SystemTimer::Minute()
//{
//    return (int)SysTime.wMinute;
//}
//
//int SystemTimer::Second()
//{
//    return (int)SysTime.wSecond;
//}
//
//int SystemTimer::MilliSecond()
//{
//    return (int)SysTime.wMilliseconds;
//}